<template>
  <div class="activity-list">
    <div class="title">我的活动列表</div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        :finished-text="`${listData.length > 0 ? '没有更多了' : ''}`"
        @load="onLoad"
      >
        <template v-if="listData.length == 0 && !loading">
          <Nodata tips="当前无活动" />
        </template>
        <template v-for="(item, index) in listData">
          <activityCard :key="index" :item="item" />
        </template>
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import activityCard from '../../components/activityCard'
import Nodata from '../../components/noData'

export default {
  components: {
    activityCard,
    Nodata
  },
  data() {
    return {
      loading: false,
      finished: false,
      refreshing: false,
      listData: [],
      params: {
        page: 1,
        pageSize: 10
      }
    }
  },
  methods: {
    onLoad() {
      this.$axios({
        method: 'post',
        url: `/activity/list/my?page=${this.params.page}&pageSize=${this.params.pageSize}`,
        data: {}
      }).then((res) => {
        if (this.refreshing) {
          this.listData = []
          this.refreshing = false
        }
        // 加载状态结束
        this.loading = false
        this.refreshing = false
        const d = (res && res.data && res.data.data) || {
          list: [],
          totalPage: 0
        }
        // 数据全部加载完成
        const { list = [] } = d
        this.listData.push(...list)
        if (d.totalPage <= this.params.page) {
          this.finished = true
        }
        this.params.page += 1
      })
    },
    onRefresh() {
      this.params.page = 1
      // 清空列表数据
      this.finished = false
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.refreshing = true
      this.onLoad()
    }
  }
}
</script>

<style scoped>
.title {
  font-size: 16px;
  font-family: PingFangSC-Medium;
  font-weight: 500;
  color: #333;
  line-height: 20px;
  background: #f0f2f5;
  padding: 12px 16px 0;
}
</style>
